//包裹类
module game {
    export class Package {
        type: number
        items: ItemComponent[]
        size: number
        emptySize: number
        public constructor(opts) {
            this.type = opts.type || 1
            this.items = opts.items || []
            this.size = opts.size || 20;
            this.emptySize = opts.emptySize || this.size
        }
        
        //初始化背包
        initPackag = function() {
            this.emptySize = this.size
            for(var i in this.items) {
                var itemComponent = this.items[i]
                if(!itemComponent) {
                    continue
                }
                itemComponent = new ItemComponent(itemComponent)
                this.items[i] = itemComponent
                this.emptySize -= 1 //空格子数量
            }
        }

        //添加物品
        addItem = function(itemComponent: ItemComponent): number {
            //判断物品是否可以叠加
            var maxstack = totalConfig[itemComponent.id].maxStack
            if(maxstack === 1) {
                //不可叠加，添加到空格子
                return this._addItemToPosition(itemComponent)
            }
            //查找物品是否已经存在
            var item = this.getItemComponent(itemComponent.id)
            if(!item) {
                //不存在，添加到空格子
                return this._addItemToPosition(itemComponent)
            } else {
                item.stack += itemComponent.stack
            }
            return 0
        }

        //获取某一物品的总数量
        getTotalNumByItemId = function(id: number): number {
            var item = this.getItemComponent(id)

            if(item === null) {
                return 0
            }
            return item.stack
        }

        //移除包裹中的物品
        removeNumByItemId = function(id: number,num: number): number {
            var index = this.getPosById(id)
            if(index === -1) {
                return num
            }
            var itemComponent: ItemComponent = this.items[index]
            var stack = itemComponent.stack
            if(stack < num) {
                this.items[index] = null
                this.emptySize += 1
                return num - stack
            } else if(stack > num) {
                itemComponent.stack -= num
            } else if(stack === num) {
                this.items[index] = null
                this.emptySize += 1
            }
            return 0
        }
        
        //移除包裹中的物品
        removeItemByPos = function(pos: number) {
            this.items[pos] = null
            this.emptySize += 1
        }
        
        //移除包裹中的物品
        removeItemById = function(id: number) {
            var index = this.getPosById(id)
            if(index === -1) {
                return
            }
            this.removeItemByPos(index)
        }
        
        //根据id返回物品的位置
        getPosById = function(id): number {
            for(var i in this.items) {
                if(this.items[i] && this.items[i].id === id) {
                    return i
                }
            }
            return -1
        }

        getItemComponent = function(id): ItemComponent {
            for(var i in this.items) {
                if(this.items[i] && this.items[i].id === id) {
                    return this.items[i]
                }
            }
            return null
        }
        //添加物品到空格子
        _addItemToPosition = function(itemComponent: ItemComponent): number {
            var pos = this._getEmpty() //获取空格子的下标
            if(pos === -1) {
                return -1
            }
            this.emptySize -= 1 //空格子数量减一
            this.items[pos] = itemComponent
            return 0
        }

        //获取一个空格子的下标
        _getEmpty = function(): number {
            if(this.emptySize === 0) {
                return -1
            }
            for(var i = 0;i < this.size;i++) {
                if(!this.items[i]) {
                    return i
                }
            }
        }

        //交换物品的位置
        transPositionItems = function(pos: number,topos: number) {
            var fromItemComponent = this.items[pos]
            var toItemComponent = this.items[topos]
            this.items[pos] = toItemComponent
            this.items[topos] = fromItemComponent
        }

        //包裹整理
        arrangePackage = function() {
            //items排序
            this.items.sort(function(item1: ItemComponent,item2: ItemComponent) {
                if(item1 && item2) {
                    if(item1.id === item2.id) {
                        return item1.stack >= item2.stack ? 1 : -1
                    } else {
                        return item1.id > item2.id ? 1 : -1
                    }
                } else if(!item1 && item2) {
                    return 1
                } else if(item1 && !item2) {
                    return -1
                } else {
                    return 1
                }
            })
            this.initPackag()
        }
        
      
    }
}

